import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录"
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: "/",        //布局
    name: "layout",
    component: () => import(/* webpackChunkName: "layout" */ '../components/Layout'),
    children: [
      {
        path: "",
        redirect: "/newsList",
        meta: {
          title: "重定向",
          hidden: true
        }
      },
      {
        path: "newsList",
        name: "newsList",
        meta: {
          title: "基础资源库",
          icon: require("@/assets/newsList.png"),
          icon_active: require("@/assets/newsListActive.png")
        },
        component: () => import(/* webpackChunkName: "newsList" */ '../views/NewsList.vue')
      },
      {
        path: "newsType",
        name: "newsType",
        meta: {
          title: "预警预报",
          icon: require("@/assets/newsType.png"),
          icon_active: require("@/assets/newsTypeActive.png"),
        },
        component: () => import(/* webpackChunkName: "newsType" */ '../views/NewsType.vue')
      },
      {
        path: "userList",
        name: "userList",
        meta: {
          title: "基础信息查询统计"
        },
        component: () => import(/* webpackChunkName: "userList" */ '../views/UserList.vue')
      },
      {
        path: "money",
        name: "money",
        meta: {
          title: "视频监控"
        },
        component: () => import(/* webpackChunkName: "money" */ '../views/Money.vue')
      },
      {
        path: "test",
        name: "test",
        meta: {
          title: "环境监测"
        },
        component: () => import(/* webpackChunkName: "test" */ '../views/Test.vue')
      },
      {
        path: "WorkerInfo",
        name: "workerInfo",
        meta: {
          title: "员工信息",
          // hidden: true
        },
        component: () => import(/* webpackChunkName: "workerInfo" */ '../views/WorkerInfo.vue')
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log("to", to);
  // 修改标题
  document.title = to.meta.title

  // 跳转前判断用户是否登录
  if (localStorage.getItem("token")) {
    next()
  } else {
    if (to.path == "/login") {
      next()
    } else {
      next("/login")
    }
  }
})

export default router
